﻿package src.uipackage.utils {
	
	/**
	* Creates an array of objects with x and y properties based on constructor arguments.
	* @author Devon O.
	*/
	public dynamic class OBO_Grid extends Array {
		
		private var _cols:int;
		private var _rows:int;
		
		/**
		 * OBO_Grid returns an array of objects with x and y coordinates (properties) based on the number of columns,
		 * rows, height and width of cells, and the x and y value of the upper left corner.
		 * 
		 * @example The following code creates a new 10x10 grid with 50x50 cells and a top left corner at (0, 0):
		 *	<listing version="3.0" >
		 * var myGrid:OBO_Grid = new OBO_Grid(10, 10, 50, 50, 0, 0);
		 * </listing>
		 * 
		 * @param	The number of columns
		 * @param	The number of rows
		 * @param	The height of the grid cell
		 * @param	The width of the grid cell
		 * @param	The x position of the top left corner of the grid
		 * @param 	The y position of the top left corner of the grid
		 * 
		 * @return  A OBO_Grid object.
		 */
		 
		public function OBO_Grid (cols:int, rows:int, cellHeight:int, cellWidth:int, x:Number = 0, y:Number = 0) {
			_cols = cols;
			_rows = rows;
			
			var numCells:int = cols * rows;
			var curx:Number = x;
			
			for (var i:int = 0; i < numCells; i ++) {
				this[i] = {x:curx, y:y};
				curx += cellWidth;
				if (!((i + 1) % cols)) {
					curx = x;
					y += cellHeight;
				}
			}
		}
		
		/**
		 * The OBO_Grid.shuffle() method will rearrange elements of the array of grid coordinates in a random order.
		 */
		public function shuffle ():void {
			var len:int = this.length;
			var temp:Object = {};
			for (var i:int = 0; i < len; i ++) {
				var rand:int = Math.floor (Math.random() * len);
				temp = this[i];
				this[i] = this[rand];
				this[rand] = temp;
			}
		}
		
		
		/**
		 * Returns the current number of columns in the grid instance.
		 */
		public function get columns():int {
			return _cols;
		}
		
		/**
		 * Returns the current number of rows in the grid instance.
		 */
		public function get rows():int {
			return _rows;
		}
	}
}
